package com.apts.interceptor;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.apts.controller.Controller_PlanB_;
import com.apts.jfinalbase.BaseInterceptor;
import com.apts.jfinalbase.BaseJsonPojo;
import com.apts.pojo.ErrorPojo;
import com.jfinal.aop.Invocation;

/**
 * 全局权限拦截器
 * 
 * @author lwh
 * @date 2016-03-20
 */
public class GlobalAuthInterceptor extends BaseInterceptor {
	public static Logger log4j = Logger.getLogger(Controller_PlanB_.class);

	@Override
	public void intercept(Invocation inv) {
		String path = inv.getViewPath() + inv.getMethodName();
		log4j.info("正在访问：" + path);
		
		// 开放通过的API
		boolean flag = (
					path.equals("/index/index")
				||	path.equals("/api/planb/doLogin")
				|| path.equals("/api/planb/wapInfo")
				|| path.equals("/api/planb/getCode")
				|| path.equals("/api/planb/isLogined")
				
				|| path.equals("/QRcode/QRcodeScan2")
				|| path.equals("/QRcode/QRcodeGenerate")
			);
		if (flag) {
			inv.invoke();
			return;
		} else {
			String account = (String) inv.getController().getSession().getAttribute("account");
			if (account != null) {
				inv.invoke();
			}else{
				BaseJsonPojo msgJson = new ErrorPojo();

				Map<String, Object> result = new HashMap<String, Object>();
				result.put("isLogined", false);
				result.put("msg", "未登录。");
				msgJson.setMsg("未登录。");
				msgJson.setContent(result);
				
				inv.getController().renderJson(msgJson);
			}
		}
		
	}
}
